<template>
    <div id="app">
        <el-row>
            <el-col :span="2">
                <el-image style="width: 100px; height: 130px" :src="photo" fit="fill"></el-image>
            </el-col>
            <el-col :span="4">
                <el-alert :title="deviceLiveTile" :type="devices.length === 0 ? 'error' : 'success'" effect="dark">
                </el-alert>
                <div v-if="devices.length !== 0">
                    <div v-for="(item, index) in devices" :key="index">
                        编号：
                        <el-input disabled placeholder="编号" v-model="item.index">
                        </el-input>
                        版本:
                        <el-input disabled placeholder="版本" v-model="item.version">
                        </el-input>
                    </div>
                </div>
            </el-col>
        </el-row>


        <el-input ref="saveTagInput" v-model="gmsfhm"></el-input>
        <el-button @click="startReadCard">身份证</el-button>
        <el-button @click="getDevice">设备</el-button>
    </div>
</template>

<script>
import axios from "axios";

export default {
    data() {
        return {
            baseUrl: "http://127.0.0.1:6045",
            readerIndex: undefined,
            devices: [], // 设备列表
            deviceLiveTile: '设备未上线',
            gmsfhm: "",
            photo: ""
        }
    },
    name: 'KtButton',
    computed: {},
    methods: {
        //設備
        startReadCard() {
            axios({
                type: "get",
                dataType: "json",
                timeout: 10000,
                crossDomain: true,
                url: this.baseUrl + "/readIDCard?index=" + this.readerIndex,
            }).then(response => {
                if (response.data.result === 0) {
                    const wzInfo = this.parseCardWzxx(response.data.wzInfo);
                    if (response.data.zpWlt) {
                        this.getPhoto(response.data.zpWlt)
                    }
                    this.gmsfhm = wzInfo.gmsfhm
                } else {
                    this.$message.error(response.data.errorMsg);
                }
            })

        },
        getPhoto(zpWlt) {
            axios({
                type: "get",
                dataType: "json",
                timeout: 10000,
                url: this.baseUrl + "/wltUnpack?wlt=" + zpWlt + "&format=bmp",
            }).then(response => {
                if (response.data.result === 0) {
                    this.photo = `data:image/bmp;base64,${response.data.image}`
                }
            })
        },
        //身份證
        getDevice() {
            axios({
                type: "get",
                dataType: "json",
                timeout: 10000,
                crossDomain: true,
                url: this.baseUrl + "/getCardReaderList",
            }).then(response => {
                if (response.data.length !== 0) {
                    this.devices = response.data
                    this.deviceLiveTile = '设备已上线'
                } else {
                    this.devices = []
                    this.deviceLiveTile = '设备已离线'
                }
            })
        },
        // 解析
        parseCardWzxx(wzxx) {
            let result = {};

            result['xm'] = wzxx.substr(0, 15);
            result['xbdm'] = wzxx.substr(15, 1);
            result['mzdm'] = wzxx.substr(16, 2);
            result['csrq'] = wzxx.substr(18, 8);
            result['dzmc'] = wzxx.substr(26, 35);
            result['gmsfhm'] = wzxx.substr(61, 18);
            result['qfjgmc'] = wzxx.substr(79, 15);
            result['qfjgmc'] = wzxx.substr(79, 15);
            result['yxqqsrq'] = wzxx.substr(94, 8);
            result['yxqjzrq'] = wzxx.substr(102, 8);
            result['zjlxbs'] = wzxx.substr(124, 1);

            switch (result['xbdm']) {
                case '1':
                    result['xbmc'] = '男';
                    break;
                case '2':
                    result['xbmc'] = '女';
                    break;
                default:
                    result['xbmc'] = '未知';
            }

            if (result['zjlxbs'] === 'J') {
                result['txzhm'] = wzxx.substr(110, 9);
                result['qfcs'] = wzxx.substr(119, 2);
            }

            switch (result['mzdm']) {
                case '01':
                    result['mzmc'] = '汉';
                    break;
                case '02':
                    result['mzmc'] = '蒙古';
                    break;
                case '03':
                    result['mzmc'] = '回';
                    break;
                case '04':
                    result['mzmc'] = '藏';
                    break;
                case '05':
                    result['mzmc'] = '维吾尔';
                    break;
                case '06':
                    result['mzmc'] = '苗';
                    break;
                case '07':
                    result['mzmc'] = '彝';
                    break;
                case '08':
                    result['mzmc'] = '壮';
                    break;
                case '09':
                    result['mzmc'] = '布依';
                    break;
                case '10':
                    result['mzmc'] = '朝鲜';
                    break;
                case '11':
                    result['mzmc'] = '满';
                    break;
                case '12':
                    result['mzmc'] = '侗';
                    break;
                case '13':
                    result['mzmc'] = '瑶';
                    break;
                case '14':
                    result['mzmc'] = '白';
                    break;
                case '15':
                    result['mzmc'] = '土家';
                    break;
                case '16':
                    result['mzmc'] = '哈尼';
                    break;
                case '17':
                    result['mzmc'] = '哈萨克';
                    break;
                case '18':
                    result['mzmc'] = '傣';
                    break;
                case '19':
                    result['mzmc'] = '黎';
                    break;
                case '20':
                    result['mzmc'] = '傈僳';
                    break;
                case '21':
                    result['mzmc'] = '佤';
                    break;
                case '22':
                    result['mzmc'] = '畲';
                    break;
                case '23':
                    result['mzmc'] = '高山';
                    break;
                case '24':
                    result['mzmc'] = '拉祜';
                    break;
                case '25':
                    result['mzmc'] = '水';
                    break;
                case '26':
                    result['mzmc'] = '东乡';
                    break;
                case '27':
                    result['mzmc'] = '纳西';
                    break;
                case '28':
                    result['mzmc'] = '景颇';
                    break;
                case '29':
                    result['mzmc'] = '柯尔克孜';
                    break;
                case '30':
                    result['mzmc'] = '土';
                    break;
                case '31':
                    result['mzmc'] = '达翰尔';
                    break;
                case '32':
                    result['mzmc'] = '仫佬';
                    break;
                case '33':
                    result['mzmc'] = '羌';
                    break;
                case '34':
                    result['mzmc'] = '布朗';
                    break;
                case '35':
                    result['mzmc'] = '撒拉';
                    break;
                case '36':
                    result['mzmc'] = '毛南';
                    break;
                case '37':
                    result['mzmc'] = '仡佬';
                    break;
                case '38':
                    result['mzmc'] = '锡伯';
                    break;
                case '39':
                    result['mzmc'] = '阿昌';
                    break;
                case '40':
                    result['mzmc'] = '普米';
                    break;
                case '41':
                    result['mzmc'] = '塔吉克';
                    break;
                case '42':
                    result['mzmc'] = '怒';
                    break;
                case '43':
                    result['mzmc'] = '乌孜别克';
                    break;
                case '44':
                    result['mzmc'] = '俄罗斯';
                    break;
                case '45':
                    result['mzmc'] = '鄂温克';
                    break;
                case '46':
                    result['mzmc'] = '德昂';
                    break;
                case '47':
                    result['mzmc'] = '保安';
                    break;
                case '48':
                    result['mzmc'] = '裕固';
                    break;
                case '49':
                    result['mzmc'] = '京';
                    break;
                case '50':
                    result['mzmc'] = '塔塔尔';
                    break;
                case '51':
                    result['mzmc'] = '独龙';
                    break;
                case '52':
                    result['mzmc'] = '鄂伦春';
                    break;
                case '53':
                    result['mzmc'] = '赫哲';
                    break;
                case '54':
                    result['mzmc'] = '门巴';
                    break;
                case '55':
                    result['mzmc'] = '珞巴';
                    break;
                case '56':
                    result['mzmc'] = '基诺';
                    break;
                case '59':
                    result['mzmc'] = '穿青人';
                    break;
                case '60':
                    result['mzmc'] = '革家人';
                    break;
                case '97':
                    result['mzmc'] = '其它';
                    break;
                case '98':
                    result['mzmc'] = '入籍';
                    break;
                case '99':
                    result['mzmc'] = '其它';
                    break;
                default:
                    result['mzmc'] = '';
            }
            return result;
        }

    },
    mounted() {
        this.$refs.saveTagInput.focus();
        this.getDevice()

    }
};
</script>
